package com.boredom.Sloth;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

import dummyObjects.DummyClub;
import dummyObjects.DummyUser;

public class SlothItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	/**This is a dummy overlay for testing;
	 * it will rapidly become more complicated*/
	private ArrayList<OverlayItem> testOverlays = new ArrayList<OverlayItem>();

	/**The displayList contains the overlays to actually bother rendering.
	 * It is filled each time the size() method is called.*/
	private ArrayList<OverlayItem> displayList;
	
	private ArrayList<DummyUser> userList;
	private ArrayList<DummyClub> clubList;

	private Context overlayContext;

	public SlothItemizedOverlay(Drawable defaultMarker){
		super(boundCenterBottom(defaultMarker));

		displayList = new ArrayList<OverlayItem>();
		userList = new ArrayList<DummyUser>();
		clubList = new ArrayList<DummyClub>();
	}

	public SlothItemizedOverlay(Drawable defaultMarker, Context context){
		super(boundCenterBottom(defaultMarker));
		overlayContext = context;

		displayList = new ArrayList<OverlayItem>();
		userList = new ArrayList<DummyUser>();
		clubList = new ArrayList<DummyClub>();
	}

	/**
	 * Clear and rebuild the display list of Overlay items.
	 * 
	 * Call this when you have changed the visibility status of Clubs or Users.*/
	public void updateDisplayList(){
		
		/**Clear the display list so we can rebuild it*/
		this.displayList.clear();
		ArrayList<String> currentMemberList = null;
		
		for(DummyClub currentClub : this.clubList ){
			
			if(currentClub.isVisible()){
			
				currentMemberList = currentClub.getMembers();
				for(DummyUser currentUser : this.userList){
				
					if(currentUser.isVisible()
					   && currentMemberList.indexOf(currentUser.getName()) >= 0){
						
						OverlayItem newOverlay = new OverlayItem(currentUser.getLocation(),
																 currentUser.getName(),
																 currentUser.getStatus());
						displayList.add(newOverlay);
					}
										
				}		
			}
		}
		
		
		populate();
	}

	public void addUser(DummyUser user){
		userList.add(user);
	}
	
	public void addClub(DummyClub club){
		clubList.add(club);
	}
	
	public void clubVisibility(String clubName, boolean isVisible){
		
		boolean match = false;
		int index = 0;
		for(DummyClub currentClub : this.clubList){
			
			if(currentClub.getName().equals(clubName)){
				match = true;
				break;
			} else {
				index++;
			}
		}
		
		if(match){
			this.clubList.get(index).setVisibility(isVisible);
		}
		
	}
	
	@Override
	protected OverlayItem createItem(int i){
		return displayList.get(i);
	}

	/**
	 * Return the number of overlays to draw.  Only visible overlays
	 * are counted and drawn.
	 * */
	@Override
	public int size(){
		
		return displayList.size();
	}

	@Override
	protected boolean onTap(int index){

		OverlayItem item = testOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(overlayContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}


}
